package com.hd.tiyu.yuejian;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hd.tiyu.yuejian.Activity.RegisterActivity;
import com.hd.tiyu.yuejian.JavaBean.Login;
import com.hd.tiyu.yuejian.Model.UserModel;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static android.R.attr.id;


public class LoginActivity extends AppCompatActivity {
    private EditText username;
    private EditText pass;
    private Button login_btn;
    private  String user_id=null;
    private TextView login_zhuce_btn;
    private SharedPreferences.Editor editor;
    private SharedPreferences sp;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.login);
        initViews();
        initEvents();
        initData();


    }

    private void initViews() {
        username=(EditText) findViewById(R.id.login_tel);
        pass=(EditText) findViewById(R.id.login_paw);
        login_btn=(Button) findViewById(R.id.button);
        login_zhuce_btn=(TextView) findViewById(R.id.zhuce);
    }

    private void initEvents() {
        login_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String tel=username.getText().toString();
                final String password=pass.getText().toString();
                final UserModel usermodel=new UserModel();
                Call<Login> call=usermodel.UserLogin(tel,password);
                call.enqueue(new Callback<Login>() {
                    @Override
                    public void onResponse(Call<Login> call, Response<Login> response) {
                            Login user=response.body();
                            if (!"tel".equals(user.getUser_name())){
                            String SessionID = user.getSessionid();
                            user_id=user.getId();
                            Toast.makeText(LoginActivity.this, "登录成功"+user_id,Toast.LENGTH_SHORT).show();
                            Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                            SharedPreferences mSharedPreferences=getSharedPreferences("loginUser", Context.MODE_PRIVATE);
                            SharedPreferences.Editor editor=mSharedPreferences.edit();
                            editor.putString("user_id",user_id);
                            editor.putString("SessionID",SessionID);
                            editor.commit();
                            startActivity(intent);

                            finish();
                        }else {
                            Toast.makeText(LoginActivity.this, "用户名或者密码错误",Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<Login> call, Throwable t) {

                    }
                });
            }
        });

        login_zhuce_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
                finish();
            }
        });

    }


    private void initData() {


    }
}
